package com.global.dao.impl;

import com.global.dao.RoleDao;
import com.global.model.Role;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * This class interacts with Spring's HibernateTemplate to save/delete and
 * retrieve Role objects.
 *
 * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> 
 */
@Repository
public class RoleDaoImpl extends GenericDaoHibernate<Role, Long> implements RoleDao {

    /**
     * Constructor to create a Generics-based version using Role as the entity
     */
    public RoleDaoImpl() {
        super(Role.class);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
	public Role getRoleByName(String rolename) {
    	if(StringUtils.isNotBlank(rolename)){
    		List<Role> roles = getHibernateTemplate().find("from Role where name=?", rolename);
            if (!roles.isEmpty()) {
                return (Role) roles.get(0);
            }
    	}
        
    	return null;
    }

    /**
     * {@inheritDoc}
     */
    public void removeRole(String rolename) {
        Object role = getRoleByName(rolename);
        getHibernateTemplate().delete(role);
    }
}
